package de.hijacksoft.isle.classes;

import android.content.Context;
import de.hijacksoft.isle.R;

/**
 *a class to manage and convert prices with different units
 * @author HiJack Soft
 */
public class Price implements Comparable<Price> {

    // Constants
    private static final String TAG = "Price";
    public double amount;   // public because one propably doesnt want to use methods to add/multiply a single double value
    private Context context; // needs a context to access strings (R.*)

    public Price(Context context, double amount) {
        this.context = context;
        this.amount = amount;
    }

    @Override
    public String toString() {

        String smallCurrency = context.getString(R.string.currency_small);
        String currency = context.getString(R.string.currency);

        int a = (int) (amount * 100); // multiply the amount by 100 and convert to int to have the digits we want to show
        String str = "";

        if (a / 100 >= 1) { // if the amount is > 1 add a digit representing the rounded value to the final string
            str = a / 100 + "";
        }

        if (a == 0) { // if the amount is 0
            return "0" + currency;
        }

        if (a % 100 == 0 && a / 100 >= 1) { // if the amount is larger than 1 but has no decimal digits
            str += currency;
            return str;
        }

        if (a % 100 != 0 && a / 100 >= 1) { // if the amount is larger than 1 and has decimal digits
            str += ".";
            String comma = "" + a % 100;
            if (comma.length() < 2) { // change single decimal numbers: 0.3 = 0.30
                comma += "0";
            }
            str += comma;
            str += currency;
            return str;
        }

        if (a % 100 != 0 && a / 100 < 1 && !smallCurrency.equals("none")) { // if the amount is smaller than 1 and has decimal digits and there is a proper small currency
            return a % 100 + smallCurrency;
        } else {
            str += "0.";
            String comma = "" + a % 100;
            if (comma.length() < 2) {
                comma += "0";
            }
            str += comma;
            str += currency;
            return str;
        }

    }

    @Override
    public int compareTo(Price another) {
        return (int) (amount * 100 - another.amount * 100);
    }
}
